package rs.dalao.syc.user.all.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rs.dalao.syc.common.pojo.authentication.CurrentPrincipal;
import rs.dalao.syc.common.response.JsonResult;
import rs.dalao.syc.user.all.pojo.param.UserPersonalAddInfo;
import rs.dalao.syc.user.all.pojo.param.UserUpdateParam;
import rs.dalao.syc.user.all.pojo.vo.UserPersonalListVO;
import rs.dalao.syc.user.all.service.IUserPersonalService;
import rs.dalao.syc.user.all.service.IUserService;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import java.io.File;

@RestController
@RequestMapping("/user/info/")
@Api(tags = "1.增加用户真实信息")
public class UserPersonalController {
    @Autowired
    private IUserPersonalService iUserPersonalService;

    @Autowired
    private IUserService userService;

    //根据用户id遍历用户真实信息展示在前端
    @GetMapping("list")
    @ApiOperation("展示用户真实信息")
    @PreAuthorize("hasAuthority('/user/info')")
    public JsonResult ListInfo(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        UserPersonalListVO userPersonalListVO = iUserPersonalService.selectInfoById(currentPrincipal.getId());
        return JsonResult.ok(userPersonalListVO);
    }

    //添加或者修改用户的真实信息
    @PostMapping("add")
    @ApiOperation("添加及修改用户真实信息")
    @PreAuthorize("hasAuthority('/user/info')")
    public JsonResult insert(UserPersonalAddInfo userPersonalAddInfo, @ApiIgnore @AuthenticationPrincipal CurrentPrincipal currentPrincipal){
        iUserPersonalService.insert(userPersonalAddInfo,currentPrincipal.getId());
        return JsonResult.ok();
    }

    //删除头像
    @PostMapping("/remove")
    @ApiOperation("删除用户头像")
    public JsonResult remove(String url){
        //删除对应的磁盘中的文件
        /* new File("d:/files"+url).delete();*/
        boolean isDelete = new File(url).delete();
        return JsonResult.ok(isDelete);
    }

    //更改用户用户头像、昵称信息
    @PostMapping("/update")
    @ApiOperation("更新用户头像、昵称")
    public JsonResult update(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal, UserUpdateParam userUpdateParam){
        userService.updateUserInfo(userUpdateParam,currentPrincipal.getId());
        return JsonResult.ok();
    }
}
